package com.qyt.hotel.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qyt.hotel.config.GlobalProperties;
import com.qyt.hotel.entity.Result;
import com.qyt.hotel.entity.StatusCode;
import com.qyt.hotel.entity.User;
import com.qyt.hotel.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private GlobalProperties globalProperties;


    @GetMapping("/getOne")
    public Result getOne(User user) {
        User user1 = adminService.getOne(user);
        if (null != user1) {
            return new Result("查询成功", true, StatusCode.OK, user1);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @GetMapping("/list")
    public Result getAdminList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, @RequestParam("roleId") Integer roleId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<User> adminList = adminService.getAdminList(roleId);
        // 封装查出的信息,并设置连续显示的页数
        PageInfo pageInfo = new PageInfo(adminList, 5);
        return new Result("查询成功", true, StatusCode.OK, pageInfo);
    }


    @PostMapping("/uploadImg")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {
        // 获得原始文件名+格式
        String OriginalFileName = file.getOriginalFilename();
        if (null != OriginalFileName) {
            // 截取文件名
            String fileName = OriginalFileName.substring(0, OriginalFileName.lastIndexOf("."));
            // 截取文件格式
            String fileType = OriginalFileName.substring(OriginalFileName.lastIndexOf("."));
            //获取当前时间(精确到毫秒)
            Date date = new Date();
            String formatDate = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date);
            //原文件名+当前时间作为新文件名
            String fileNewName = formatDate + (new Random()).nextInt(10000) + fileType;
            File f = new File(globalProperties.getAdminStaticPath());
            if (!f.exists()) {
                boolean mkdirs = f.mkdirs();
            }
            if (!file.isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(globalProperties.getAdminStaticPath() + fileNewName);
                    InputStream in = file.getInputStream();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = in.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Result("上传异常", false, null, null);
                }
            }
            return new Result("上传成功", true, null, fileNewName);
        } else {
            return new Result("上传失败", false, null, null);
        }
    }

    @PostMapping("/add")
    public Result addAdmin(User user) {
        Integer count = adminService.addAdmin(user);
        if (null != count) {
            return new Result("添加成功", true, StatusCode.OK, null);
        } else {
            return new Result<>("添加失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/delete")
    public Result deleteAdmin(User user) {
        Integer count = adminService.deleteAdmin(user);
        String photo = user.getPhoto();
        File f = new File(String.format("%s%s", globalProperties.getAdminStaticPath(), photo));
        boolean flag = f.delete();
        if (null != count) {
            return new Result("删除成功", true, StatusCode.OK, null);
        } else {
            return new Result("删除失败", false, StatusCode.ERROR, null);
        }
    }

    @GetMapping("/update")
    public Result updateAdmin(User user) {
        Integer count = adminService.updateAdmin(user);
        if (null != count) {
            return new Result("修改成功", true, StatusCode.OK, null);
        } else {
            return new Result("修改失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/resetPass")
    public Result resetPass(@RequestParam("id") Integer id, @RequestParam("oldPass") String oldPass, @RequestParam("newPass") String newPass) {
        String passWord = adminService.getPassWord(id);
        System.out.println(passWord);
        System.out.println(newPass);
        if (oldPass.equals(passWord)) {
            Integer count = adminService.resetPass(id, newPass);
            if (count != null) {
                return new Result("修改成功", true, StatusCode.OK, null);
            } else {
                return new Result("修改失败", false, StatusCode.ERROR, null);
            }
        } else {
            return new Result("旧密码输入错误", false, StatusCode.ERROR, null);
        }
    }
}
